# MD5（13位时间戳+8位十进制随机数字+版本号+signKey）后转16进制大写字符串
#
# 注意：sign拼接顺序不能改变，以下为示例
# var sign = MD5(timestamp + nonce + version + manageId + signKey).toUpperCase()
import string
import hashlib
import random
import time
import requests

testAdress = 'http://apigame.tt.sanwenqian.cn'
timeStamp = str(int(round(time.time()*1000)))
nonce = ''.join(random.sample(string.digits, 8))
version = '1.0'
manageId = "1"
signKey = 'LWQZRWMTKOHQO5BMFIW7WDTWUSBGG1TA'
token  = 'atEDA+mULrCnHg5ITOGO5A=='

str = timeStamp+nonce+version+manageId+signKey

def md5forsign(str):
    md = hashlib.md5()
    md.update(str.encode())
    res = md.hexdigest().upper()
    return res

sign = md5forsign(str)
print(nonce, timeStamp, sign)

# 用户授权
authorization_url = testAdress+'/Z_Api/v1/user/authorization'

data = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "manageId": manageId,
  "fields": {
    "jsCode":"071tCeX30hpvlK1XyPX30QSTW30tCeXW",
    "iv":"566ROFLl+pndbUnqz1Ykaw==",
    "signature": "5d8b40abd8a4fea1d3756da8207dc0812093229e",
    "encryptedData": "/fOioQI2XGhM+tzmgu0Mxe5Sq0rksamHFudj7P5GmwwT2IRXix/UOIUbRgazF12sA0i4fy1mSHFGp+6PTt8Fm1e6fVYvGbSQN72nS25m3EtDT4SX3qTzqOYhQIYPMxLwI94MbwvdUDpcvAJk6rSIkF7p9vvuWRdr1r5U1+q6S4+Fr22P6yQXds6BmhpjaUMqNhaHY9H5o3ZC79gjAv5j4Q86JV+6xr4+mJ1wZwQ86aOoXOfXr3h4LhHjiZq0KpdYk61MBvLc29Rw3swM5hwsJOti1k6HhmXUHZuw86k0se8CFoCvQ2rxC7Dtup9Li63Of6gFeR6PYPhRIl23WQ99vHAPJ0MAM5jmPpWzKIvEGtu2nrjDeXKuEwrcyhQoqv5HtgNYA6IKWbeZOY4HKdHQuAI4vTSopcOjmbUoG2SW6lMUOpZhs7rhVbQfaT41deXZ4oJ65CGbjrM1U/7y9vySDzc8DAZKHbO0BipEHt5kEdw=",
    "rawData": '{"nickName":"鲸与海","gender":2,"language":"zh_CN","city":"","province":"North Shore","country":"New Zealand","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIyRC437akiblHeSRuz1RQ6icJ0R0qM6GCN5IMNmmONHcwX9NxGbcRDQqzV1WKSvdTgfJVUmCMicxZfg/132"}'
  }
}

try:
  loginResponse = requests.post(authorization_url, json=data).json()
except Exception as e:
  print('++++++++++++++++', e)
finally:
  print('用户授权:', loginResponse)




# 记录用户访问次数
visit_url = testAdress + '/Z_Api/v1/user/visit'

dataKong = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "manageId": 1,
  "fields": {}
}

visitRes = requests.post(visit_url, json=dataKong)
print('记录用户访问次数:', visitRes.json())

# 记录用户参与次数
play_url = testAdress + '/Z_Api/v1/user/play'

playRes = requests.post(play_url,json=dataKong)
print('记录用户参与次数:', playRes.json())

# 查询剩余积分
score_url = testAdress+'/Z_Api/v1/user/getScores'
dataPage={
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "manageId": 1,
  "fields": {
    "page": 1,
    "pageSize":10
  }
}
scoreRes = requests.post(score_url,json=dataPage)
print('查询剩余积分:', scoreRes.json())

# 查询积分记录
scoreRecord_url = testAdress+'/Z_Api/v1/user/getScoreRecords'
data = dataPage
scoreRecordRes = requests.post(scoreRecord_url,json=data)
print('查询积分记录:', scoreRecordRes.json())

# 查询兑换记录
swapRecords_url = testAdress+'/Z_Api/v1/user/getSwapRecords'
data = dataPage
swapRecordsRes = requests.post(swapRecords_url, json=data)
print('查询兑换记录:', swapRecordsRes.json())

# 查询是否存在手机号
existMobile_url = testAdress+'/Z_Api/v1/user/existMobile'
data=dataKong
existMobileRes = requests.post(existMobile_url, json=dataKong)
print('查询是否存在手机号:', existMobileRes.json())

# 兑换奖品
swapPrize_url = testAdress+'/Z_Api/v1/user/swapPrize'
print('-------', swapPrize_url)
swapPrize_data = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "manageId": 1,
  "fields": {
    "mobile": "18810789836",
    "prizeId": 1
  }
}

swapPrizeRes = requests.post(swapPrize_url, json=swapPrize_data)
print('兑换奖品:', swapPrizeRes.json())

# 获得分享信息

getShareInfo_url = testAdress+'/Z_Api/v1/user/getShareInfo'

getShareInfoRes = requests.post(getShareInfo_url, json=dataKong)
print('获得分享信息', getShareInfoRes.json())

# 查询当天积分
queryScores_url = testAdress+'/Z_Api/v1/game/queryScores'
queryScoresRes = requests.post(queryScores_url, json=dataKong)
print('查询当天积分:', queryScoresRes.json())

# 获得问题
queryQuestions_url = testAdress+'/Z_Api/v1/game/queryQuestions'
questionsData = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "manageId": 1,
  "fields": {
    "sceneType": 1
  }
}

queryQuestionsRes = requests.post(queryQuestions_url, json=questionsData)
print('获得问题:', queryQuestionsRes.json())

# 获得回答结果
getResult_url = testAdress + '/Z_Api/v1/game/getResult'
print(getResult_url)
getResultData = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "manageId": 1,
  "fields": {
    "sceneType": 1,
    "questionId": 1255,
    "answer": "海绵宝宝"
  }
}
getResultRes = requests.post(getResult_url, json=getResultData)
print('获得回答结果:', getResultRes.json())

# 分享群增加复活机会
